package com.mdata.controller;

import com.mdata.model.po.CacheUser;
import com.mdata.common.RedisKeyConstant;
import com.mdata.model.po.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;

@Controller
public class LoginController2 {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping({"/", "index"})
    public String toIndex(Model model){
        model.addAttribute("message", "蜜度企业微信管理平台" + LocalDate.now());
        return "index";
    }

    @GetMapping("/user/add")
    public String add(Model model){
        return "user/add";
    }

    @GetMapping("/user/update")
    public String update(){
        return "user/update";
    }

    @GetMapping("/toLogin2")
    public String toLogin(){
        return "login";
    }

    @PostMapping("/login2")
    public String login(String userName, String password, Model model){
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);

        Subject subject = SecurityUtils.getSubject();

        try{
            subject.login(token);
            User user = (User) subject.getPrincipals().getPrimaryPrincipal();
            CacheUser cacheUser = CacheUser.builder()
                    .token(subject.getSession().getId().toString())
                    .build();
            BeanUtils.copyProperties(user, cacheUser);

            // 一个账号只能一个人登录
            String key = RedisKeyConstant.LOGIN_USER_TOKEN + user.getId();
            if(redisTemplate.hasKey(key)){
                redisTemplate.delete(RedisKeyConstant.SESSION_KEY_PREFIX + redisTemplate.opsForValue().get(key));
                redisTemplate.delete(key);
            }
            redisTemplate.opsForValue().set(key, cacheUser.getToken());

            System.out.println("===========");
            System.out.println(cacheUser);
            System.out.println("===========");
            return "redirect:/index";
        }catch (UnknownAccountException exception){
            model.addAttribute("msg", "账号错误");
        }catch (IncorrectCredentialsException exception){
            model.addAttribute("msg", "密码错误");
        }
        return "login";
    }

    @GetMapping("/logout2")
    public String logout(HttpSession session, Model model) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        model.addAttribute("msg","安全退出！");
        return "login";
    }
}
